# Maintainer: Alexey Pavlov <Alexpux@gmail.com>
# Contributor: Martell Malone <martellmalone@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=minizip
pkgbase=mingw-w64-${_realname}-git
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
replaces=("${MINGW_PACKAGE_PREFIX}-minizip")
provides=("${MINGW_PACKAGE_PREFIX}-minizip")
_base_ver=1.2
pkgver=1.2.445.e67b996
pkgrel=1
pkgdesc="Compression library implementing the deflate compression method found in gzip and PKZIP (mingw-w64)"
arch=('any')
license=(ZLIB)
url="https://www.zlib.net/"
depends=("${MINGW_PACKAGE_PREFIX}-bzip2" "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "git"
             "${MINGW_PACKAGE_PREFIX}-cmake")
options=('staticlibs')
source=("git+https://github.com/nmoinvaz/minizip#branch=1.2"
        010-unzip-add-function-unzOpenBuffer.patch
        011-minizip-cmake-pkgconfig.patch
        012-minizip-all-in-one-library.patch
        013-fix-cmake-destination.patch)
sha256sums=('SKIP'
            'dcc6138a806fede86aa469fdb28afe4b3206272cf5530c1888fd7986f9c46e9b'
            '7856b9aabb0d8bfd4d43dc9a3e6cd8561f61af2089069579907225583f613a03'
            '7323d55572be4da277e586405ea0c68be1d41e62686c7e247a237d5a6c2f3e2d'
            '5062c51dd10626e179f5b3f3e55225c62390f6ba1f38dd4ef70b4fb7ba6926b2')

pkgver() {
  cd "${srcdir}"/${_realname}
  printf "%s.%s.%s" ${_base_ver} "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

prepare() {
  cd ${srcdir}/${_realname}
  patch -p1 -i ${srcdir}/010-unzip-add-function-unzOpenBuffer.patch
  patch -p1 -i ${srcdir}/011-minizip-cmake-pkgconfig.patch
  patch -p1 -i ${srcdir}/012-minizip-all-in-one-library.patch
  patch -p1 -i ${srcdir}/013-fix-cmake-destination.patch
}

build() {
  cd "${srcdir}"/${_realname}
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi
  
  mkdir -p build-shared
  pushd build-shared > /dev/null
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${extra_config[@]}" \
    -DBUILD_SHARED_LIBS=ON \
    -DDEF_ZLIB_ROOT=${MINGW_PREFIX} \
    ../
  make
  popd > /dev/null

  mkdir -p build-static
  pushd build-static > /dev/null
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${extra_config[@]}" \
    -DBUILD_SHARED_LIBS=OFF \
    -DDEF_ZLIB_ROOT=${MINGW_PREFIX} \
    ../
  make
  popd > /dev/null
}

package() {
  cd ${srcdir}/${_realname}
  pushd build-static > /dev/null
    make DESTDIR="${pkgdir}" install
  popd > /dev/null

  pushd build-shared > /dev/null
    make DESTDIR="${pkgdir}" install
  popd > /dev/null
}
